package com.example.tliasmanagement.service.impl;

import com.example.tliasmanagement.dto.AddStudentDto;
import com.example.tliasmanagement.dto.StudentListDto;
import com.example.tliasmanagement.dto.UpdateStudentDto;
import com.example.tliasmanagement.entity.EmpLog;
import com.example.tliasmanagement.entity.Student;
import com.example.tliasmanagement.exception.BizException;
import com.example.tliasmanagement.mapper.StudentMapper;
import com.example.tliasmanagement.service.EmpLogService;
import com.example.tliasmanagement.service.StudentService;
import com.example.tliasmanagement.vo.PageBean;
import com.example.tliasmanagement.vo.StudentVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private EmpLogService logService;

    /**
     * 分页查询
     */
    @Override
    public PageBean students(StudentListDto dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        List<Student> list = studentMapper.page(dto);
        Page<Student> p = (Page<Student>) list;
        return new PageBean(p.getTotal(), p.getResult());
    }

    /**
     * 班级查询
     */
    @Override
    public List<StudentVo> getClazzNames() {
        List<StudentVo> list = studentMapper.select();
        return list;
    }

    /**
     * 添加学员
     */
    @Override
    @Transactional(rollbackFor = Exception.class) //AOP
    public void addStudent(AddStudentDto dto) {
        boolean success = false;
        // 判断学号是否重复
        Student studentDB = studentMapper.selectByNo(dto.getNo());
        if(studentDB != null){
            // 说明 学号重复
            throw new BizException("学号重复");
        }
        studentDB = studentMapper.selectByPhone(dto.getPhone());
        if(studentDB != null){
            // 说明 手机号是否重复
            throw new BizException("手机号重复");
        }
        studentDB = studentMapper.selectByIdCard(dto.getIdCard());
        if(studentDB != null){
            // 说明 身份证号是否重复
            throw new BizException("身份证号重复");
        }

        //try{
            Student student = new Student();
            BeanUtils.copyProperties(dto, student);
            student.setCreateTime(LocalDateTime.now());
            student.setUpdateTime(LocalDateTime.now());
            studentMapper.insert(student);

           /* success = true;
            // 提交事务
        }catch (Exception e) {
            success = false;
            throw new RuntimeException();
            //回滚事务
        } finally {
            // 记录日志
            EmpLog empLog = new EmpLog();
            empLog.setOperateTime(LocalDateTime.now());
            empLog.setInfo("添加员工" + (success ? "成功" : "失败"));
            logService.insertLog(empLog);
        }*/

    }

    /**
     * 删除学员
     */
    @Override
    public void deleteById(List<Integer> ids) {
        studentMapper.deleteById(ids);
    }

    /**
     * 根据ID查询学员
     */
    @Override
    public Student getById(Integer id) {
        Student student = studentMapper.selectById(id);
        return student;
    }

    /**
     * 修改学员
     */
    @Override
    public void updateStudent(UpdateStudentDto dto) {
        Student student = new Student();
        student.setName(dto.getName());
        student.setNo(dto.getNo());
        student.setGender(dto.getGender());
        student.setPhone(dto.getPhone());
        student.setIdCard(dto.getIdCard());
        student.setIsCollege(dto.getIsCollege());
        student.setAddress(dto.getAddress());
        student.setDegree(dto.getDegree());
        student.setGraduationDate(dto.getGraduationDate());
        student.setClazzId(dto.getClazzId());
        student.setId(dto.getId());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }

    /**
     * 违纪处理
     */
    @Override
    public void updateScore(Integer id, Integer score) {
        studentMapper.updateScore(id, score);
    }
}
